/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import olympicgameslogic.AppUser;

/**
 *
 * @author Nuno /* To change this template, choose Tools | Templates and open
 * the template in the editor.
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import olympicgameslogic.*;

/**
 *
 * @author fabio mendonca
 */
public class DialogManageEdition extends JDialog {

   private JTextField txtYear = new JTextField(15);
   private JTextField txtCity = new JTextField(15);
   private JTextField txtoffDesign = new JTextField(15);
   JButton button, buttonExit;
   private Edition edition;
   private ImageIcon img;
   private String tit;
   int year;
   private DialogEditionsMain tempDialog;
   ClassLoader cldr = this.getClass().getClassLoader();
   DialogAppUsers da;

   public DialogManageEdition(JFrame pai, String titulo, Edition ed, DialogEditionsMain dialEd) {


      super(pai, titulo, true);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      this.setLocation(pai.getX() + 100, pai.getY() + 100);

      tempDialog = dialEd;
      if (ed == null) {
	 img = new ImageIcon(cldr.getResource("images/extra/user_add.png"));
	 tit = "Add Edition";
      } else {
	 img = new ImageIcon(cldr.getResource("images/extra/user_edit.png"));
	 tit = "Edit Edition";
	 edition = ed;
	 txtCity.setText(edition.getCity());
	 txtoffDesign.setText(edition.getOfficialDesignation());
	 txtYear.setText(Integer.toString(edition.getYear()));

      }


      setLayout(new BorderLayout());
      setBackground(Color.white);

      //      Criação da barra de topo
      JPanel pannorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pannorte.setSize(200, 60);
      pannorte.setBackground(Color.white);
      JLabel background = new JLabel(tit);
      background.setFont(new Font("Arial", Font.BOLD, 14));
      background.setIcon(img);
      pannorte.add(background);





      JPanel pancentro = new JPanel();
      pancentro.setBackground(Color.white);
      pancentro.setLayout(new GridLayout(3, 1));


      JPanel p0 = new JPanel(new GridLayout(1, 2));
      p0.add(new JLabel("City"));

      p0.add(txtCity);
      p0.setBackground(Color.white);
      pancentro.add(p0);

      JPanel p1 = new JPanel(new GridLayout(1, 2));
      p1.add(new JLabel("Official Designation"));

      p1.add(txtoffDesign);
      p1.setBackground(Color.white);
      pancentro.add(p1);

      JPanel p2 = new JPanel(new GridLayout(1, 2));
      p2.add(new JLabel("Year"));
      p2.add(txtYear);


      p2.setBackground(Color.white);
      pancentro.add(p2);




      JPanel pansul = new JPanel();
      //pansul.setSize(200, 20);
      pansul.setBackground(Color.white);
      pansul.setLayout(new FlowLayout(FlowLayout.CENTER));
      button = new JButton((ed == null ? "Add" : "Update"), new ImageIcon(cldr.getResource("images/extra/btok.png")));

      button.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (txtCity.getText().trim().equals("")) {
	       JOptionPane.showMessageDialog(DialogManageEdition.this, "City name is mandatory",
		       getTitle(), JOptionPane.WARNING_MESSAGE);
	       txtCity.requestFocus();
	    } else if (txtoffDesign.getText().trim().equals("")) {
	       JOptionPane.showMessageDialog(DialogManageEdition.this, "Official designation is mandatory",
		       getTitle(), JOptionPane.WARNING_MESSAGE);
	       txtoffDesign.requestFocus();
	    } else {
	       try {
		  year = Integer.parseInt(txtYear.getText().trim());
	       } catch (NumberFormatException nfe) {
		  txtYear.setText("");
		  JOptionPane.showMessageDialog(DialogManageEdition.this, "Invalid year", getTitle(), JOptionPane.WARNING_MESSAGE);
		  txtYear.requestFocus();
		  return;
	       }
	       if (button.getText().equalsIgnoreCase("add")) {
		  if (OlympicGamesGui.ap.getEdition(year) == null) {
		     OlympicGamesGui.ap.addEdition(new Edition(year, txtCity.getText().trim(), txtoffDesign.getText().trim(), null));
		     tempDialog.refreshTable();
		     edition = null;
		     txtYear.setText("");
		     txtoffDesign.setText("");
		     txtCity.setText("");
		     button.setText("Add");
		  } else {
		     JOptionPane.showMessageDialog(DialogManageEdition.this, "Edition already exists!", getTitle(), JOptionPane.WARNING_MESSAGE);
		     txtYear.requestFocus();
		     return;
		  }



	       } else if (button.getText().equalsIgnoreCase("update")) {
		  if (edition.getYear() == year) {
		     edition.setCity(txtCity.getText().trim());
		     edition.setOfficialDesignation(txtoffDesign.getText().trim());
		     tempDialog.refreshTable();
		     edition = null;
		     txtYear.setText("");
		     txtoffDesign.setText("");
		     txtCity.setText("");
		     button.setText("Add");
		     
		     

		  } else {
		     if (OlympicGamesGui.ap.getEdition(year) == null) {
			edition.setYear(year);
			edition.setCity(txtCity.getText().trim());
			edition.setOfficialDesignation(txtoffDesign.getText().trim());
			tempDialog.refreshTable();
			edition = null;
			txtYear.setText("");
			txtoffDesign.setText("");
			txtCity.setText("");
			button.setText("Add");
			tit = "Add Edition";
		     }else
		     {
			JOptionPane.showMessageDialog(DialogManageEdition.this, "Edition already exists!", getTitle(), JOptionPane.WARNING_MESSAGE);
		     txtYear.requestFocus();
		     return;
		     }

		  }

	       }


	    }
	 }
      });
      pansul.add(button);

      buttonExit = new JButton("Exit", new ImageIcon(cldr.getResource("images/extra/btcancel.png")));

      buttonExit.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    setVisible(false);
	 }
      });
      pansul.add(buttonExit);


      add(pannorte, BorderLayout.NORTH);
      add(pancentro, BorderLayout.CENTER);
      add(pansul, BorderLayout.SOUTH);
      // setSize(340, 180);
      setResizable(false);
      pack();

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);









   }

   public void showJan() {

      this.setVisible(true);


   }
}
